package com.sloera.factory;

import com.sloera.api.ServiceMapped;
import com.sloera.dto.RPCTransformObj;
import com.sloera.net.BIOClient;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class RPCInvocationHandler implements InvocationHandler {
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //System.out.println(method);
        //for (Object arg :
        //        args) {
        //    System.out.println(arg);
        //}
        RPCTransformObj rpcTransformObj = new RPCTransformObj();
        rpcTransformObj.setMethodName(method.getName());
        rpcTransformObj.setParams(args);
        Class<?> clazz = method.getDeclaringClass();
        String classPath = clazz.getDeclaredAnnotation(ServiceMapped.class).value();
        rpcTransformObj.setClassPath(classPath);
        return BIOClient.callRemoteProcedure("localhost", 8888, rpcTransformObj);
    }
}
